import { anyItem } from '../types/order'
import { IntegralItem } from '../types/integral'
import { BaseResponse, PageResult } from '../types/common'
import { CouponResponse, CouponData } from '../types/coupon'
import { mockBillList, fetchIntegalList, mockorderListData, mockIntegralList, mockCouponList, fetchBillList, mockStoreCouponList } from './mock'
import { PageParams, CouponListParams, IntegralListParams } from '../types/api'
import { OrderItem, OrderStatus } from '../types/order'

export interface API {
  getBillList(params: PageParams): Promise<BaseResponse<PageResult<anyItem>>>
  getIntegralList(params: PageParams): Promise<BaseResponse<PageResult<IntegralItem>>>
  getCouponList(params: PageParams): Promise<CouponResponse>
  getOrderList(params: PageParams): Promise<BaseResponse<PageResult<OrderItem>>>
  getOrderDetail(id: string): Promise<BaseResponse<anyItem>>
}

class ApiService implements API {
  async getBillList(params: PageParams): Promise<BaseResponse<PageResult<anyItem>>> {
    await new Promise(resolve => setTimeout(resolve, 1000))
	const mockBillList = fetchBillList();
    const { page, pageSize } = params
    const start = (page - 1) * pageSize
    const end = start + pageSize
    const list = mockBillList.slice(start, end)
    return {
      code: 0,
      message: 'success',
      data: {
        list,
        total: mockBillList.length,
        hasMore: end < mockBillList.length
      }
    }
  }

  async getIntegralList(params: PageParams): Promise<BaseResponse<PageResult<IntegralItem>>> {
    await new Promise(resolve => setTimeout(resolve, 1000))
	const mockIntegralList = fetchIntegalList();
    const { page, pageSize } = params
    const start = (page - 1) * pageSize
    const end = start + pageSize
    const list = mockIntegralList.slice(start, end)
    console.log(list,'====')
    return {
      code: 0,
      message: 'success',
      data: {
        list,
        total: mockIntegralList.length,
        hasMore: end < mockIntegralList.length
      }
    }
  }

  async getCouponList(params: PageParams): Promise<CouponResponse> {
    const { level } = params
    
    await new Promise(resolve => setTimeout(resolve, 1000))

    return {
      code: 0,
      message: 'success',
      data: mockCouponList as CouponData[],
      storeList: mockStoreCouponList as CouponData[]
    }
  }

  async getOrderList(params: PageParams): Promise<BaseResponse<PageResult<OrderItem>>> {
    const transformedList = mockorderListData();
    const { page, pageSize } = params
    const start = (page - 1) * pageSize
    const end = start + pageSize

    const list:any = transformedList.slice(start, end)
    
    return {
      code: 0,
      message: 'success',
      data: {
        list,
        total: transformedList.length,
        hasMore: end < transformedList.length
      }
    }
  }

  async getOrderDetail(id: string): Promise<BaseResponse<anyItem>> {
    const order = mockBillList.find((item: anyItem) => item.id === id)
    return {
      code: 0,
      message: 'success',
      data: order || mockBillList[0] // Fallback to first item if not found
    }
  }

  private async mockRequest<T>(response: BaseResponse<T>): Promise<BaseResponse<T>> {
    await new Promise(resolve => setTimeout(resolve, 1000))
    return response
  }
}

export const api = new ApiService()

export const logout = async (): Promise<boolean> => {
  try {
    // Add your actual logout API call here if needed
    uni.setStorageSync('_login', false)
    return true
  } catch (error) {
    return false
  }
} 